source('base.R')

analyse <- function(pos_data, neg_data) {
    pos <- data.frame(tag='p', data=pos_data)
    neg <- data.frame(tag='n', data=neg_data)

    result <- aov(data~tag, data=rbind(pos,neg))
    print(summary(result))
    print(model.tables(result,"means"),digits=3)
}

run <- function(log_filename, profile_filename, output_filename) {
    log <- read(log_filename)
    profile <- read(profile_filename)

    user_data <- data.frame(user_id=as.integer(levels(factor(log$user_id))))
    
    user_data$count <- table(log$user_id)
    user_data$dur <- duration(log)
    user_data$p_ratio <- pos_action_ratio(log)

    user_data$ban <- profile$ban[match(user_data$user_id, profile$user_id)]
    user_data$heart <- profile$heart[match(user_data$user_id, profile$user_id)]
    user_data$play <- profile$play[match(user_data$user_id, profile$user_id)]
    user_data$artist <- profile$artist[match(user_data$user_id, profile$user_id)]

    user_data <- user_data[complete.cases(user_data),]

    positive_user <- user_data[user_data$p_ratio>=0.9,]
    negative_user <- user_data[user_data$p_ratio<=0.1,]

    sink(output_filename)
    analyse(positive_user$count, negative_user$count)
    analyse(positive_user$dur, negative_user$dur)
    analyse(positive_user$ban, negative_user$ban)
    analyse(positive_user$heart, negative_user$heart)
    analyse(positive_user$play, negative_user$play)
    analyse(positive_user$artist, negative_user$artist)
    sink()
}
